<template>
  <el-dialog
    title="分配角色"
    :visible="showRoleDialog"
    @close="closeDialog"
  >
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="item in list"
        :key="item.id"
        :label="item.id"
        @changed="console.log('roleIds')"
      >
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="submitRoles">
          确定
        </el-button>
        <el-button size="small" @click="closeDialog">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRolesList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: [], // 角色列表
      roleIds: []
    }
  },
  created() {
    this.getRolesList()
    // this.getUserDetailById(this.userId)
  },
  methods: {
    closeDialog() {
      this.roleIds = []
      this.$emit('update:showRoleDialog', false)
    },
    async getRolesList() {
      const { rows } = await getRolesList()
      this.list = rows
    },
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id)
      this.roleIds = roleIds // 赋值本用户的角色
      console.log(123, this.roleIds)
    },
    async submitRoles() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      this.closeDialog()
    }
  }
}
</script>
